//
//  UIView+ScrollToMiddle.m
//  JuanRoobo
//
//  Created by Zhi Kuiyu on 15/6/11.
//  Copyright (c) 2015年 Zhi Kuiyu. All rights reserved.
//

#import "UIView+ScrollToMiddle.h"
#import <objc/runtime.h>
#import "UITextField+InputText.h"
#import "UIView+Handle.h"
@implementation UIView (ScrollToMiddle)
@dynamic tapGesture;

#pragma mark - set get method


- (void)setTapGesture:(UITapGestureRecognizer *)tapGesture{
    objc_setAssociatedObject(self, @"tapGesture", tapGesture, OBJC_ASSOCIATION_RETAIN);
}

- (UITapGestureRecognizer *)tapGesture{
    return objc_getAssociatedObject(self, @"tapGesture");
}

- (void)setEndBlock:(void (^)(UIView *))EndBlock{
    objc_setAssociatedObject(self, @"endBlock", EndBlock, OBJC_ASSOCIATION_RETAIN);
}

- (void (^)(UIView *))EndBlock{
    return objc_getAssociatedObject(self, @"endBlock");
}


- (void)handleSingleTap:(UITapGestureRecognizer *)recognizer {
//    if([self viewController].clildIsFirstResponse){
//       
//        [[UIApplication sharedApplication] sendAction:@selector(resignFirstResponder) to:nil from:nil forEvent:nil];
//        [self RecoveryStartLayout];
//    }
}

#pragma mark - my method


- (void)scrollToVisiable{
//    [self viewController].clildIsFirstResponse = YES;
    UIViewController * con = [self viewController];
    
    
    CGRect rect = [self convertRect:self.bounds toView:con.view] ;
    float height = [self viewController].view.height;

    float viewheight = CGRectGetMaxY(rect);
    if(viewheight > height){
        rect.origin.y -= (viewheight - height) ;
    }
    
    float sycenter = CGRectGetMidY(rect);
    float volatileHeight = 190;
    
    if(height > 500){
        volatileHeight = 255;
    }
    
    float toTop = MAX(MIN(volatileHeight - sycenter, 0), (300 - height));

    [UIView animateWithDuration:.3 animations:^{
        [self viewController].view.top = toTop ;
//        if([con isKindOfClass:[NavBaseViewController class]]){
//            ((NavBaseViewController *) con).navBarBg.top = -toTop;
//        }
    }];
    
    [[self viewController].view removeGestureRecognizer:self.tapGesture];
    self.tapGesture = nil;
    
    
    UITapGestureRecognizer *singleFingerTap =
    [[UITapGestureRecognizer alloc] initWithTarget:self
                                            action:@selector(handleSingleTap:)];
    [[self viewController].view addGestureRecognizer:singleFingerTap];
    self.tapGesture = singleFingerTap;
    
    
}

- (void)RecoveryStartLayout{
//    [self viewController].clildIsFirstResponse = NO;
    [[self viewController].view removeGestureRecognizer:self.tapGesture];

    if(self.EndBlock){
        self.EndBlock (self);
    }
    self.EndBlock = nil;
    [UIView animateWithDuration:.25 animations:^{
        [self viewController].view.top = 0 ;
//        if([self.viewController isKindOfClass:[NavBaseViewController class]]){
//            ((NavBaseViewController *) self.viewController).navBarBg.top = 0;
//        }
    }];
    self.tapGesture = nil;
    
}

@end
